package com.lucker.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.PatternSyntaxException;

import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;

public class StringUtil extends StringUtils{
	
	public static boolean isJsonString(String text) {
		try {
			if(isEmpty(text))return Boolean.FALSE;
			JSONObject o = JSONObject.parseObject(text);
			o.getClass();
			return Boolean.TRUE;
		}catch(Exception e) {
			return Boolean.FALSE;
		}
	}
	
//这个方法有BUG暂时不使用	
//	public static boolean isJsonString(String text, Class<?> clz) {
//		try {
//			if(isEmpty(text))return Boolean.FALSE;
//			User u =(User)JSONObject.parseObject(text, clz);
//			
//			System.out.println(u.getAa() + "---" + u.getCc() + "----" + (u == null));
//			
//			return Boolean.TRUE;
//		}catch(Exception e) {
//			return Boolean.FALSE;
//		}
//	}
	
	public static void main(String[] args) {
		Map<String,Object> _map = new HashedMap<String,Object>();
		_map.put("aa", "bb");
		_map.put("cc", "dd");
		String js = JSONObject.toJSONString(_map);
		
//		System.out.println(isJsonString(js+1));
		
		
//		System.out.println(isJsonString(js,User.class));
		
	}
	
	
	
	public static List<String> stringToList(String str,String spl){
		String[] arr = str.split(spl);
		if(arr == null || arr.length == 0) {
			return null;
		}
		return Arrays.asList(arr); 
	}
	
	
	

	
	
		
		
	public static List<Integer> stringToListInteger(String str,String spl){
		if(StringUtil.isBlank(str))return null;
		String[] arr = null;
		try {
			arr = str.split(spl);
		}catch (PatternSyntaxException e) {
			arr = str.split("\\" + spl);
		}
		
		if(arr == null || arr.length == 0) {
			return null;
		}
		
		List<Integer> retList = new ArrayList<Integer>();
		for(String s : arr) {
			retList.add(Integer.parseInt(s));
		}
		return retList;
	}	
		
		
	
	public static ArrayList<Integer> stringToArrayListInteger(String str,String spl){
		if(StringUtil.isBlank(str))return null;
		String[] arr = null;
		try {
			arr = str.split(spl);
		}catch (PatternSyntaxException e) {
			arr = str.split("\\" + spl);
		}
		if(arr == null || arr.length == 0) {
			return null;
		}
		
		@SuppressWarnings("unused")
		ArrayList<Integer> retList = new ArrayList<Integer>();
		for(String s : arr) {
			retList.add(Integer.parseInt(s));
		}
		return retList;
	}	
	
	public static Set<Integer> stringToArraySetInteger(String str,String spl){
		if(StringUtil.isBlank(str))return null;
		String[] arr = null;
		try {
			arr = str.split(spl);
		}catch (PatternSyntaxException e) {
			arr = str.split("\\" + spl);
		}
		if(arr == null || arr.length == 0) {
			return null;
		}
		
		@SuppressWarnings("unused")
		Set<Integer> set = new HashSet<Integer>();
		for(String s : arr) {
			set.add(Integer.parseInt(s));
		}
		return set;
	}
	
	
	public static String trim(String args,char beTrim) {
		int st = 0;
		int len = args.length();
		char[] val = args.toCharArray();
		char sbeTrim = beTrim;
		while ((st < len) && (val[st] <= sbeTrim)) {
		  st++;
		}
		while ((st < len) && (val[len - 1] <= sbeTrim)) {
		  len--;
		}
		return ((st > 0) || (len < args.length())) ? args.substring(st, len) : args;
	}
	
	
	
	
}
